package com.itheima.test.job;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.core.util.ShardingUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class HelloJob {
//    @Scheduled(cron = "0/5 * * * * ?")
@XxlJob("helloJob")
    public ReturnT eat(String params){
        System.out.println("5秒中吃一次饭，我想成为一个胖子"+new Date());
        return ReturnT.SUCCESS;
//        ReturnT.FAIL;
    }


    @XxlJob("subJob")
    public ReturnT subJob(String params){
        System.out.println("这个是子任务==》"+ LocalDateTime.now().toString());
        return ReturnT.SUCCESS;
//        ReturnT.FAIL;
    }

    /**
     * 2、分片广播任务
     */
    @XxlJob("shardingJobHandler")
    public ReturnT<String> shardingJobHandler(String param) throws Exception {

        // 分片参数
        ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
        XxlJobLogger.log("分片参数：当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());

        List<Integer> list = getList();
        for (Integer integer : list) {
            if(integer % shardingVO.getTotal() == shardingVO.getIndex()){
                System.out.println("第"+shardingVO.getIndex()+"分片执行，执行数据为："+integer);
            }
        }

        return ReturnT.SUCCESS;
    }

    public static List<Integer> getList(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000 ; i++) {
            list.add(i);
        }
        return list;
    }
}